import Api from '../api/index'
import * as types from './types'
import { Message } from 'element-ui'
import VueRouter from 'vue-router'
// import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-default/index.css'
const router = new VueRouter();


export const localLogin = (store, userInfo) => {
    Api.authLogin(userInfo).then((response) => {
        if(response.data.code===200){
            getUserInfo(store);
            console.log(localStorage.token);
            localStorage.removeItem('token');
            localStorage.setItem("token",JSON.stringify(response.data.object.token));
            console.log(localStorage.token);
            store.commit(types.LOGIN_SUCCESS, {token: response.data.object.token });
            Message.success(response.data.msg);
            router.push({path:'/'});
        }else{
            Message.error(response.data.msg);
        }
    }, (response) => {
        Message.error('登录失败');
    })
};

export const getUserInfo = (store) => {
    Api.getUserInfo().then((response) => {
        if(response.data.code===200){
            store.commit(types.USERINFO_SUCCESS, { user: response.data.object.user });
        }else {
            return store.commit(types.USERINFO_FAILURE)
        }
    }, (response) => {
        store.commit(types.USERINFO_FAILURE)
    })
}

export const logOut = (store) => {
    return store.commit(types.LOGOUT_USER);
};

export const getUser = (store) => {
    console.log(store.state.auth.user);

};